package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.ChartDAO;
import com.zerosky.crm.po.ChartSales;
import com.zerosky.crm.po.CustomerGrowth;
import com.zerosky.crm.po.ExportAllChartSales;
import com.zerosky.crm.service.ChartService;
import com.zerosky.crm.util.DateUtil;

@Service(value="chartService")
public class ChartServiceImpl implements ChartService {
	@Autowired
	private ChartDAO chartDao;

	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<ChartSales> getChartSaleList(Map<String, Object> map) {
		String date = (String) map.get("date");
		if (date == "thisMonth" || "thisMonth".equals(date)) {
			map.put("date", DateUtil.getThisMonth());
		}
		if (date == "lastMonth" || "lastMonth".equals(date)) {
			map.put("date", DateUtil.getLastMonth());
		}
		if (date == "thisYear" || "thisYear".equals(date)) {
			map.put("date", DateUtil.getThisYear());
		}
		if (date == "month" || "month".equals(date)) {
			String m = (String) map.get("month");
			map.put("date", DateUtil.getMonthOfThisYear(m));
		}
		if (date == "1" || "1".equals(date)) {
			map.put("date", null);
			map.put("quarter", "1");
		}
		if (date == "2" || "2".equals(date)) {
			map.put("date", null);
			map.put("quarter", "2");
		}
		if (date == "3" || "3".equals(date)) {
			map.put("date", null);
			map.put("quarter", "3");
		}
		if (date == "4" || "4".equals(date)) {
			map.put("date", null);
			map.put("quarter", "4");
		}
		return chartDao.getChartSaleList(map);

	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<ChartSales> getRankChartList(Map<String, Object> map) {
		String date = (String) map.get("date");
		if ("thisMonth".equals(date)) {
			map.put("date", DateUtil.getThisMonth());
		} else if ("lastMonth".equals(date)) {
			map.put("date", DateUtil.getLastMonth());
		} else if ("thisYear".equals(date)) {
			map.put("date", DateUtil.getThisYear());
		} else if ("month".equals(date)) {
			String m = (String) map.get("month");
			map.put("date", DateUtil.getMonthOfThisYear(m));
		} else if ("1".equals(date)) {
			map.put("quarter", "1");
		} else if ("2".equals(date)) {
			map.put("quarter", "2");
		} else if ("3".equals(date)) {
			map.put("quarter", "3");
		} else if ("4".equals(date)) {
			map.put("quarter", "4");
		}
		return chartDao.getRankChartList(map);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<ChartSales> getSumSalesList(Map<String, Object> map) {
		return chartDao.getSumSalesList(map);
	}

	/**
	 * 客户增长统计图表
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<CustomerGrowth> getCustomerGrowthChart(Map<String, Object> map) {
		return chartDao.getCustomerGrowthChart(map);
	}

	/**
	 * 插入销售数据
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int saveChatSales(ChartSales chartSales) {
		return chartDao.saveChatSales(chartSales);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<ExportAllChartSales> getExportAllChartSales(Map<String, Object> map) {
		return chartDao.getExportAllChartSales(map);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<ExportAllChartSales> getExportAllCustomer(Map<String, Object> map) {
		return chartDao.getExportAllCustomer(map);
	}

	/**
	 * 部门销售额数据接口
	 * 
	 * @author 钟璟;
	 * @since 2017-06-19
	 */
	public ChartSales querySumMoney(Map<String, Object> map) {
		return chartDao.querySumMoney(map);
	}
}
